let request = require("request");

//这里只做测试，所以用变量存，而实际应用中，应该使用数据缓存
const expiryTime = 10 * 60 * 1000;// 过期间隔时间，毫秒
let ips = null; //代理ip
let time = null;// 存储代理IP的时间，判断是否过期，如果过期重新请求

module.exports = {
    /**
     * 请求免费代理，可做缓存，这里就存在变量中，只做测试
     */
    getProxyList111: () => {
        return new Promise((resolve, reject) => {
            const nowDate = Date.now();
            if (nowDate - time < expiryTime) {
                resolve(ips);
                return;
            }
            const apiURL = 'http://www.66ip.cn/mo.php?sxb=%D5%E3%BD%AD&tqsl=100&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=http%3A%2F%2Fwww.66ip.cn%2F%3Fsxb%3D%25D6%25D0%25B9%25FA%26tqsl%3D100%26ports%255B%255D2%3D%26ktip%3D%26sxa%3D%26radio%3Dradio%26submit%3D%25CC%25E1%2B%2B%25C8%25A1';
            const options = {
                method: 'GET', url: apiURL, gzip: true, encoding: null,
                headers: {
                    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                    'Accept-Encoding': 'gzip, deflate',
                    'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4',
                    'User-Agent': 'Mozilla/8.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
                    'referer': 'http://www.66ip.cn/'
                },
            };
            request(options, (error, response, body) => {
                try {
                    if (Buffer.isBuffer(body)) {
                        const ret = body.toString().match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,4}/g);
                        ips = ret;
                        time = Date.now();
                        resolve(ret);
                    }
                } catch (e) {
                    console.log(e);
                }
            });
        })
    },
    /**
     * 请求免费代理，可做缓存，这里就存在变量中，只做测试
     */
    getProxyList: (num, time = 1) => {
        return new Promise((resolve, reject) => {
            // const nowDate = Date.now();
            // if (nowDate - time < expiryTime && ips.length) {
            //     resolve(ips);
            //     return;
            // }
            const apiURL = `http://ip.ipjldl.com/index.php/api/entry?method=proxyServer.hdtiqu_api_url&packid=0&fa=0&groupid=0&fetch_key=&time=${time}&qty=${num || 1}&port=1&format=json&ss=5&css=&dt=0&pro=&city=&usertype=4`;
            const options = {
                method: 'GET', url: apiURL, gzip: true,
                // encoding: null,
                headers: {
                    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                    'Accept-Encoding': 'gzip, deflate',
                    'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4',
                    'User-Agent': 'Mozilla/8.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
                    'referer': 'http://ip.ipjldl.com/'
                },
            };
            request(options, (error, response, body) => {
                if (body) {
                    try {
                        resolve({ success: true, body: JSON.parse(body) });
                    } catch (error) {
                        resolve({ success: true, body: { data: [], success: false } });
                    }
                } else {
                    reject({ success: false, body });
                }
            });
        })
    }
};
